package com.jd.goods.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jd.common.util.IdUtils;
import com.jd.common.util.MysqlPageUtils;
import com.jd.goods.mapper.GoodsDescriptionMapper;
import com.jd.goods.mapper.GoodsMapper;
import com.jd.goods.mapper.GoodsSpecificationMapper;
import com.jd.goods.pojo.Goods;
import com.jd.goods.pojo.GoodsDescription;
import com.jd.goods.pojo.GoodsSpecification;
import com.jd.goods.service.GoodsService;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsDescriptionMapper goodsDescriptionMapper;
	@Autowired
	private GoodsSpecificationMapper goodsSpecificationMapper;
	
	@Override
	public Goods getGoodsById(Long id) {
		// TODO Auto-generated method stub
		return goodsMapper.getGoodsById(id);
	}

	@Override
	public List<Goods> getList(int page, int rows) {
		// TODO Auto-generated method stub
		return goodsMapper.getList(new MysqlPageUtils(page, rows));
	}

	@Override
	public Long getCount() {
		// TODO Auto-generated method stub
		return goodsMapper.getCount();
	}

	@Override
	public int tx_addGoods(Goods goods, String desc, String specification) {
		goods.setId(IdUtils.genItemId());
		Date today = new Date();
		goods.setCreated(today);
		goods.setUpdated(today);
		goodsMapper.addGoods(goods);
	/*	
	 * if(1==1)
			throw new RuntimeException("测试事务回滚！！");*/
		GoodsDescription goodsDesc = new GoodsDescription();
		goodsDesc.setCreated(today);
		goodsDesc.setUpdated(today);
		goodsDesc.setGoodsId(goods.getId());
		goodsDesc.setGoodsDesc(desc);
		goodsDescriptionMapper.add(goodsDesc);
		
		GoodsSpecification record = new GoodsSpecification();
		record.setGoodsId(goods.getId());
		record.setSpecifications(specification);
		record.setCreated(today);
		record.setUpdated(today);
		return goodsSpecificationMapper.add(record);
	}

}
